<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class CatlistModelCatlist extends JModel
{
    /**
     * Gets the greeting
     * @return string The greeting to be displayed to the user
     */
    function getSections()
    {
        $db =& JFactory::getDBO();
        $sql = "SELECT id,title "
              ." FROM #__sections"
              ." ORDER BY id ASC"
              ;
        $db->setQuery($sql);
        $res = $db->loadObjectList();

        $new_res = $this->numSection($res);
        
        return $new_res;
    }

    function numSection($secs){
        $db =& JFactory::getDBO();
        foreach($secs as $sec){
            $sql = "SELECT * "
                  ." FROM #__content"
                  ." WHERE sectionid = '".$sec->id."'"
                  ;
            $db->setQuery($sql);
            $db->query();
            $numrow = $db->getNumRows();

            if($numrow > 0)
            {
                $sec->section_num = $numrow;
                $new_sec[] = $sec;
            }
        }
        return $new_sec;
    }


    /**
     *
     * @return <type>
     */
    function getCategories(){
        $secs = $this->getSections();
        $db =& JFactory::getDBO();

        foreach($secs as $section){
            $sql = "SELECT id,alias,parent_id,title,section "
                  ." FROM #__categories"
                  ." WHERE section = '".$section->id."'"
                  ." ORDER BY id ASC"
                  ;
            $db->setQuery($sql);
            $db->query();
            $numrow = $db->getNumRows();
            $res = $db->loadObjectList();

            if($numrow > 0)
            {
                $new_cat = $this->numCategory($res);
                $cat[] = $new_cat;
            }
        }
        return $cat;
    }


    function numCategory($cats){
        $db =& JFactory::getDBO();
        foreach($cats as $cat){
            $sql = "SELECT * "
                  ." FROM #__content"
                  ." WHERE sectionid = '".$cat->section."'"
                  ." AND catid = '".$cat->id."'"
                  ;
            $db->setQuery($sql);
            $db->query();
            $numrow = $db->getNumRows();

            if($numrow > 0)
            {
                $cat->category_num = $numrow;
                $new_cat[] = $cat;
            }
        }
        return $new_cat;
    }


}